﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Tasks;
using Microsoft.ISVApps.Common.Utils;
using System.Threading;

namespace Microsoft.ISVApps.Common.Controls
{
    public enum ContactAction
    {
        Call,
        Mail,
        Directions,
        Save,
        NoAction
    }

    public partial class ContactActions : UserControl
    {
       
        //Fast accessor to resources
        private ResourceAdapter<string> Res;
        
        public ContactActions()
        {
            InitializeComponent();            
            try
            {
                //HACK Designer trick
                Res = Microsoft.ISVApps.Common.Utils.Extensions.GetDefaultResourceAdapter<string>();
            }
            catch { }
        }
        
        private void PerformTask(ContactAction contactAction, bool p)
        {
            try
            {
                switch (contactAction)
                {
                    case ContactAction.Call:
                        var call = new PhoneCallTask();
                        call.DisplayName = Res("COMPANY_NAME");
                        call.PhoneNumber = Res("COMPANY_PHONE");
                        call.Show();
                        break;
                    case ContactAction.Mail:
                        var mail = new EmailComposeTask();
                        mail.Subject = Res("COMPANY_MAIL_SUBJECT");
                        mail.To = Res("COMPANY_MAIL_ADDRESS");
                        mail.Show();
                        break;
                    case ContactAction.Directions:
                        var route = new BingMapsDirectionsTask();
                        route.End = new LabeledMapLocation()
                        {
                            Label = string.Format("{0}, {1}, {2} {3},{4}",
                                Res("COMPANY_STREET"), Res("COMPANY_CITY"), Res("COMPANY_ZIP"), Res("COMPANY_STATE"), Res("COMPANY_COUNTRY"))
                        };
                        route.Show();
                        break;
                    case ContactAction.Save:
                        var card = new SaveContactTask();
                        card.Company = Res("COMPANY_NAME");
                        card.FirstName = Res("COMPANY_NAME");
                        card.Website = Res("COMPANY_WEBSITE");
                        card.WorkAddressCity = Res("COMPANY_CITY");
                        card.WorkAddressCountry = Res("COMPANY_COUNTRY");
                        card.WorkAddressState = Res("COMPANY_STATE");
                        card.WorkAddressStreet = Res("COMPANY_STREET");
                        card.WorkAddressZipCode = Res("COMPANY_ZIP");
                        card.WorkEmail = Res("COMPANY_MAIL_ADDRESS");
                        card.WorkPhone = Res("COMPANY_PHONE");
                        card.Show();
                        break;
                    case ContactAction.NoAction:
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {
                if (!p) throw;
            }                        
        }
        
        #region "Event Handlers"

        private void Call(object sender, GestureEventArgs e)
        {
            PerformTask(ContactAction.Call, false);            
        }
        private void Mail(object sender, GestureEventArgs e)
        {
            PerformTask(ContactAction.Mail, true);    
        }
        private void Maps(object sender, GestureEventArgs e)
        {
            PerformTask(ContactAction.Directions, true);
        }
        private void Save(object sender, GestureEventArgs e)
        {
            PerformTask(ContactAction.Save, true);
        }

        #endregion

        
    }
}
